<?php

namespace App\Admin\Controllers\Auth;

use App\Admin\Traits\ReponseJson;
use Illuminate\Routing\Controller as BaseController;
use App\Admin\Requests\AdminUserLoginRequest;
use Illuminate\Support\Arr;

class AuthController extends BaseController
{
    use ReponseJson;

    /**
     * 登录页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function login()
    {
        return view('admin.auth.login');
    }

    /**
     * 登录
     * @param \App\Admin\Requests\AdminUserLoginRequest $request
     * @return mixed
     */
    public function auth(AdminUserLoginRequest $request)
    {
        $account = Arr::only($request->all(), ['staff_id', 'password']);
        $admin   = auth('admin');

        if (!$admin->attempt($account)) {
            return $this->failure('帳號或錯誤錯誤');
        }

        if (!$admin->user()->enable) {
            $admin->logout();
            return $this->failure('帳號已被停用');
        }

        return $this->success('登錄成功', [], 200, request('referer', '/'));
    }

    /**
     * 登出
     * @return mixed
     */
    public function logout()
    {
        auth('admin')->logout();
        return $this->success('登出成功', [], 200, route('admin.login', [], false));
    }

}
